#!/usr/bin/env python
"""
重置学生密码为默认密码
"""
import os
import sys
import django

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'inter_api.settings')
django.setup()

from django.contrib.auth import get_user_model, authenticate
from students.models import Student

User = get_user_model()

def reset_student_password():
    """重置学生密码"""
    print("重置学生密码...")
    
    try:
        # 查找学生
        student = Student.objects.get(student_id='CS2024001')
        user = student.user
        
        if not user:
            print("❌ 学生没有关联用户账号")
            return
        
        print(f"找到用户: {user.username}")
        print(f"当前密码验证:")
        print(f"  123456: {'✅' if user.check_password('123456') else '❌'}")
        print(f"  testpass123: {'✅' if user.check_password('testpass123') else '❌'}")
        
        # 设置新密码
        new_password = '123456'
        user.set_password(new_password)
        user.save()
        
        print(f"\n✅ 已将用户 {user.username} 的密码重置为: {new_password}")
        
        # 验证新密码
        print(f"\n验证新密码:")
        print(f"  123456: {'✅' if user.check_password('123456') else '❌'}")
        
        # 测试完整认证流程
        print(f"\n=== 测试完整认证流程 ===")
        identifiers = ['CS2024001', '13800138001', 'zhangsan@opstu.edu.cn']
        
        for identifier in identifiers:
            auth_user = authenticate(username=identifier, password='123456')
            if auth_user:
                print(f"✅ {identifier} 认证成功")
            else:
                print(f"❌ {identifier} 认证失败")
                
    except Student.DoesNotExist:
        print("❌ 没有找到学号为 CS2024001 的学生")
    except Exception as e:
        print(f"❌ 重置密码失败: {e}")

if __name__ == '__main__':
    reset_student_password()
